<?php
    if(!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * Main class for working with documents.
     */
    class Documents extends CI_Controller {

        private $data;

        function __construct() {

            parent::__construct();

            if (!$this->session->userdata('logged_in'))
                redirect(base_url('login'));

            $this->load->config('languages');
            $this->load->config('gallery');// ovdje je page_types

            $this->load->model('document_model');
            $this->load->model('posts_model');

            $this->translate = & $this->factory->create('translate', 'foo', 'bar');
            $this->activeLang = $this->session->userdata('my_language');
        }

        public function index() {
            if (!isset($_GET['post_type'])) {
                $_GET['post_type'] = "page";
            }
            $this->view_all();
        }

        /**
         * 
         * @param $_GET['post_type']
         * @param $_GET['type']
         */
        function view_all() {
            if (!isset($_GET['type'])) {
                $_GET['type'] = "page";
            }
            $this->data['page'] = 'Documents';
            $this->data['title'] = 'View all Folders';
            $this->data['post_type'] = $_GET['post_type'];

            $this->data['all_folders'] = $this->document_model->view_all($_GET['type']);

//            $this->firephp->log($this->data['all_folders']);

            if ($_GET['type'] == 'all')
                $this->template->load('documents/view_all_all', $this->data);
            else
                $this->template->load('view_all', $this->data);
        }

        function view_new() {
            $this->data['page'] = 'Documents';
            $this->data['title'] = 'Add Folders / Images';
            $this->data['all_documents'] = $this->document_model->view_all(NULL);
            $this->template->load('documents/add_folder_files', $this->data);
        }

        /**
         * 
         * @param $_GET['post_ID']
         * @param $_GET['post_type']
         */
        function view_edit() {
            $this->data['post_action'] = "EDIT";
            $this->data['page'] = 'Documents';
            $this->data['title'] = 'Edit Folder';
            $this->data['post_type'] = $_GET['post_type'];
            $this->data['document'] = $this->document_model->view_edit($_GET['documents_ID']);
            $this->data['files'] = $this->document_model->view_edit_files($_GET['documents_ID']);
            $this->template->load('documents/edit_folder', $this->data);
        }

        /**
         * 
         * new-document
         * 
         * @param $_POST['post_action']
         * @param $_POST['type']
         * @param $_POST['galleryname']
         * @param $_POST['parent_id']
         * 
         *  edit-document
         * 
         * @param $_POST['post_action']
         * @param $_POST['document_ID']
         * @param $_POST['linkto']
         * @param $_POST['galleryname']
         * @param $_POST['parent_id']
         * 
         * new-files
         * 
         * @param $_POST['document_ID']
         * 
         */
        function submit_gallery() {

            $message = "Something went wrong. Please try again.";

            switch ($_POST['post_action']) {

                case 'new-gallery':

                    $result = $this->document_model->add_document(
                            $_POST['type'], $_POST['galleryname'], $_POST['parent_id']
                    );

                    if ($result)
                        $message = 'Successful add folder operation.';

                    break;

                case 'edit-gallery':

                    $result = $this->document_model->edit_document(
                            $_POST['document_ID'], $_POST['galleryname'], $_POST['linkto'], $_POST['parent_id']
                    );

                    if ($result)
                        $message = 'Successful edit document operation.';

                    break;

                case 'new-pictures':

                    //load reasize library
                    $this->load->library('img_map');

                    $result = $this->document_model->add_files(
                            $_POST['galleryselect'], $_POST['pictures'], $_POST['pic_titles']
                    );

                    if ($result)
                        redirect(base_url('documents/view_all?post_type=document&type=all'));
                    else
                        show_error('Something went wrong. Add files fails.');
            }

            echo json_encode(array('action' => true, 'msg' => $message));
        }

        function delete($ID) {

            $result = $this->document_model->delete($ID);

            if ($result)
                redirect(base_url('documents/view_all?post_type=document&type=all'));
            else
                show_error('Error ocured while deleting document!');
        }

        function delete_file() {

            $result = $this->document_model->delete_file($_GET['picture_ID'], $_GET['document_ID']);

            // Return
            if ($result)
                redirect(base_url('documents/view_edit?documents_ID=' . $_GET['document_ID'] . '&post_type=document'));
            else
                show_error('Something went wrong. Delete file fails.');
        }

        function files_order() {

            $result = $this->document_model->files_order($_POST['document_ID'], $_POST['picture-order']);


            if ($result)
                redirect(base_url('documents/view_edit?documents_ID=' . $_POST['document_ID'] . '&post_type=gallery'));
            else
                show_error('Something went wrong. Order files fails.');
        }

    }

    /* End of file documents.php */
    /* Location: ./application/controllers/documents.php */